package priv.wangy.micro.common.sync.consumer;

import lombok.Builder;
import lombok.Data;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.FactoryBean;

import java.util.Objects;

/**
 * @author zhangjq
 * @since 2023-12-18
 */
@Data
@Builder
public class ConsumerContainer implements FactoryBean<SimpleMessageListenerContainer> {

    private AmqpAdmin amqpAdmin;

    private ConnectionFactory connectionFactory;

    private Queue queue;

    private IDataSyncConsumer consumer;

    @Override
    public SimpleMessageListenerContainer getObject() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setAmqpAdmin(amqpAdmin);
        container.setConnectionFactory(connectionFactory);
        container.setQueues(queue);
        if (Objects.nonNull(consumer)) {
            container.setMessageListener(consumer);
        }
        return container;
    }

    @Override
    public Class<?> getObjectType() {
        return SimpleMessageListenerContainer.class;
    }
}
